package cn.com.infohold.smartcity.recceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;


public class ReceiverSystem extends BroadcastReceiver {

    private void PrintLog(String log) {
//        Logger.debug(log);
    }

    private static final ReceiverSystem instance = new ReceiverSystem();

    private static ReceiverSystem getInstance() {
        return instance;
    }

    private ReceiverSystem() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        switch (action) {
            case Intent.ACTION_SHUTDOWN:
                PrintLog("ACTION_SHUTDOWN");
                break;
            case Intent.ACTION_REBOOT:
                PrintLog("ACTION_REBOOT");
                break;
            case Intent.ACTION_TIME_TICK:
                PrintLog("ACTION_TIME_TICK");
                break;
            case Intent.ACTION_DATE_CHANGED:
            case Intent.ACTION_TIME_CHANGED:
                if (action.equals(Intent.ACTION_DATE_CHANGED)) {// 设置日期
                    PrintLog("ACTION_DATE_CHANGED");
                }
                if (action.equals(Intent.ACTION_TIME_CHANGED)) {// 设置时间
                    PrintLog("ACTION_TIME_CHANGED");
                }
                break;
            case Intent.ACTION_SCREEN_ON:
                PrintLog("ACTION_SCREEN_ON");
                break;
            case Intent.ACTION_SCREEN_OFF:
                PrintLog("ACTION_SCREEN_OFF");
                break;
            case Intent.ACTION_USER_PRESENT:
                PrintLog("ACTION_USER_PRESENT");
                break;
            case Intent.ACTION_CLOSE_SYSTEM_DIALOGS:
                PrintLog("ACTION_CLOSE_SYSTEM_DIALOGS");
                break;
            default:
                break;
        }
        //TODO
    }

    public static void switchBroadcast(Context context, boolean sw) {
        try {
            if (sw) {
                try {
                    context.unregisterReceiver(ReceiverSystem.getInstance());
                } catch (Exception e) {
                }
                final IntentFilter filter = new IntentFilter();
                filter.addAction(Intent.ACTION_TIME_TICK);
                filter.addAction(Intent.ACTION_TIME_CHANGED);
                filter.addAction(Intent.ACTION_DATE_CHANGED);
                filter.addAction(Intent.ACTION_BOOT_COMPLETED);
                filter.addAction(Intent.ACTION_SHUTDOWN);
                filter.addAction(Intent.ACTION_REBOOT);
                filter.addAction(Intent.ACTION_SCREEN_ON);
                filter.addAction(Intent.ACTION_SCREEN_OFF);
                filter.addAction(Intent.ACTION_USER_PRESENT);
                filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);

                context.registerReceiver(ReceiverSystem.getInstance(), filter);
            } else {
                context.unregisterReceiver(ReceiverSystem.getInstance());
            }
        } catch (final Exception e) {

        }
    }
}